﻿using Developer.Core.FrameWork.HelpFile;
using Developer.Core.FrameWork.RedisExtensions.Service;
using Developer.Core.FrameWork.RedisExtensions.Service.Impl;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace Developer.Core.FrameWork.RedisExtensions.Extensions
{
    public static class RedisServiceCollectionExtensions
    {
        public static void AddRedisService(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            services.AddTransient<IRedisBasketRepository, RedisBasketRepositoryImpl>();

            // 配置启动Redis服务，虽然可能影响项目启动速度，但是不能在运行的时候报错，所以是合理的
            services.AddSingleton<ConnectionMultiplexer>(sp =>
            {
                //获取连接字符串
                string redisConfiguration = Appsettings.app(new string[] { "RedisConfig", "ConnectionString" });

                var configuration = ConfigurationOptions.Parse(redisConfiguration, true);

                configuration.ResolveDns = true;

                return ConnectionMultiplexer.Connect(configuration);
            });
        }
    }
}
